// pages/group/eidt.js
import {getgdetail,upfile,editgroup} from "../../utils/api"
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
   
  dele(){
    var that=this;
    that.setData({
      filelist:[]
    })
  },
  casnchange(e){
    var that=this
    var index=e.target.dataset.id
    var value=e.detail
    var list=that.data.list
    list[index].num=value
    that.setData({
      list:list
    })
  },
  txtchange(e){
    var that=this
    if(e.type=='change'){
     var value=e.detail
    }else if(e.type=='click'){
     var value= e.target.dataset.name
    }else{
      var value=e.detail.value
    }
    var tlin=e.target.dataset.id
    that.setData({
      [tlin]:value
    })
   
 },
  viewimg(e){
    var that=this;
    var filelist=[];
    const { file } = e.detail;
    upfile(file).then(data=>{
        var d=JSON.parse(data);
        var newfile={
          url:d.data.fullurl,
        };
        filelist.push(newfile);
        that.setData({
          filelist:filelist
        })
    }) 
   },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that=this;
    var gid=options.gid;
    
    getgdetail(gid).then((res)=>{
      console.log(res)
       if(res.code==1){
        var filelist=[{url:res.data.detail.image}]
        console.log(filelist)
        that.setData({
            gid:gid,
            filelist:filelist,
            date:res.data.date,
            date1:res.data.date1,
            isin:res.data.isin,
            isguan:res.data.isguan,
            users:res.data.users,
            name:res.data.detail.name,
            desc:res.data.detail.desc,
            type:res.data.detail.type,
            list:res.data.detail.numjson,
            huixiang:res.data.detail.huixiang,
            statime:res.data.detail.statime,
            endtime:res.data.detail.endtime,
            maxtime:res.data.detail.maxtime,
        })
       }else{
         wx.showToast({
           title:res.msg,
           icon:"none"
         })
       }
    }) 
  },
  
  caschange(e){
    var that=this
    var index=e.target.dataset.id
    var value=e.detail
    var list=that.data.list
    list[index].tit=value
    that.setData({
      list:list
    })
  },
  epgroup(){
    console.log(this.data)
    var that=this
    var url=that.data.filelist.length==0?'':that.data.filelist[0].url
    var pdata={
      id:that.data.gid,
      name:that.data.name==undefined?'':that.data.name,
      desc:that.data.desc==undefined?'':that.data.desc,
      type:that.data.type==undefined?'':that.data.type,
      image:url,
      statime:that.data.statime==undefined?'':that.data.statime,
      endtime:that.data.endtime==undefined?'':that.data.endtime,
      numjson: JSON.stringify(that.data.list),
      huixiang:that.data.huixiang==undefined?'':that.data.huixiang,
    }
    console.log(pdata)
    editgroup(pdata).then(res=>{
      console.log(res)
      wx.showToast({
        title: res.msg,
        icon:"none"
      })
       if(res.code==1){
         wx.redirectTo({
           url: '/pages/group/detail?gid='+that.data.gid,
         })
       }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})